import router from './index'
import store from '../store'
import EditRole from "@/components/set/EditRole";
import newAddRouters from "@/router/newAddRouters";

function createRouter(menuArr) {
  const router=[]
  menuArr.forEach((item) => {

    if(item.componentPath === ''){

        router.push({
          path: item.menuUrl,
          name: item.pathName,
          meta: {requireAuth: true, menuId: item.menuId}
        })
        console.log(router)

      }
    else{

        router.push({
          path: item.menuUrl,
          name: item.pathName,
          meta: {requireAuth: true, menuId: item.menuId},
          component: resolve => require(['@/components/' + item.componentPath + ''], resolve)
        })
      console.log('当前路由地址：')
      console.log('@/components/' + item.componentPath + '');
    }
    
    //判断有没有孩子路由，递归调用
    if(item.subMenu!=null){
      if(item.subMenu.length>0){
        router.push(...createRouter(item.subMenu))
      }
    }
    
  })
  return router
}

function dynamicRouter(){
  let routers = []
  routers = createRouter(store.getters.getMenuTree)
  router.options.routes[2]= {
    path: '/index',
    // name:'index',
    meta: { requireAuth: true },
    redirect:{path:'/showPage'},
    component: () => import('../views/index.vue'),

  }
  //index的子路由
  router.options.routes[2].children = [...routers]

  router.options.routes[2].children.push(...newAddRouters.newRouters)

  //添加路由
  router.$addRoutes(router.options.routes)
  // console.log(router.options.routes)
}

export default {
  dynamicRouter:dynamicRouter
}